package cn.xie.domain.strategy.model.entity;

import cn.xie.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: xiaoxie
 * create: 2025-08-05 22:59
 * @BelongsProject: bit-market
 * @BelongsPackage: cn.xie.domain.strategy.model.entity
 * description: 策略规则实体
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class StrategyRuleEntity {
    /**
     * 抽奖策略ID
     **/
    private Long strategyId;
    /**
     * 抽奖奖品ID【规则类型为策略，则不需要奖品ID】
     **/
    private Integer awardId;
    /**
     * 抽象规则类型；1-策略规则、2-奖品规则
     **/
    private Integer ruleType;
    /**
     * 抽奖规则类型【rule_random - 随机值计算、rule_lock - 抽奖几次后解锁、rule_luck_award - 幸运奖(兜底奖品)】
     **/
    private String ruleModel;
    /**
     * 抽奖规则比值
     **/
    private String ruleValue;
    /**
     * 抽奖规则描述
     **/
    private String ruleDesc;

    /**
     * @description: 获取抽奖规则比值Map 4000:102,103,104,105 5000:102,103,104,105,106,107 6000:102,103,104,105,106,107,108,109
     * @author: xiaoxie
     * @date: 2025/8/5 下午11:18
     * @param: []
     * @return: java.util.Map<java.lang.String, java.util.List < java.lang.Integer>>
     **/
    public Map<String, List<Integer>> getRuleValueMap() {
        if (!Constants.StrategyRule.RULE_WEIGHT.equals(ruleModel)) return null;
        Map<String, List<Integer>> ruleValueMap = new HashMap<>();
        if (ruleValue == null || ruleValue.trim().isEmpty()) {
            return ruleValueMap;
        }
        // 按空格分组
        String[] groups = ruleValue.trim().split(Constants.SPACE);
        for (String group : groups) {
            if (!group.contains(Constants.COLON)) {
                continue;
            }
            String[] kv = group.split(Constants.COLON, 2);
            String ids = kv[1].trim();

            // 解析奖品ID列表
            List<Integer> idList = Arrays.stream(ids.split(Constants.SPLIT))
                    .filter(s -> !s.isEmpty())
                    .map(Integer::valueOf)
                    .collect(Collectors.toList());

            ruleValueMap.put(group, idList);
        }
        return ruleValueMap;
    }
    /**
     * @description: 获取到黑名单规则的奖品id和黑名单用户的一个map 100:user101,user102,user103
     * @author: xiaoxie
     * @date: 2025/8/8 下午10:10
     * @param: []
     * @return: java.util.Map<java.lang.Integer, java.util.List < java.lang.String>>
     **/
    public Map<Integer, Set<String>> getAwardIdBlackUserIdMap() {
        if (!Constants.StrategyRule.RULE_BLACK_LIST.equals(ruleModel)) {
            return Collections.emptyMap();
        }
        if (ruleValue == null || ruleValue.trim().isEmpty()) {
            return Collections.emptyMap();
        }

        Map<Integer, Set<String>> map = new HashMap<>();
        for (String group : ruleValue.trim().split(Constants.SPACE)) {
            if (!group.contains(Constants.COLON)) {
                continue;
            }
            String[] kv = group.split(Constants.COLON, 2);
            if (kv.length < 2 || kv[1].trim().isEmpty()) {
                continue;
            }
            int awardId;
            try {
                awardId = Integer.parseInt(kv[0].trim());
            } catch (NumberFormatException e) {
                log.warn("非法奖品ID: {}", kv[0]);
                continue;
            }

            Set<String> userIds = Arrays.stream(kv[1].split(Constants.SPLIT))
                    .map(String::trim)
                    .filter(s -> !s.isEmpty())
                    .collect(Collectors.toCollection(LinkedHashSet::new));
            map.merge(awardId, userIds, (oldSet, newSet) -> {
                oldSet.addAll(newSet);
                return oldSet;
            });
        }
        // 返回不可变视图，外部无法修改
        return Collections.unmodifiableMap(map);
    }
}
